/*
 * @Company: TWL
 * @Author: xue jian
 * @Email: xuejian@kanzhun.com
 * @Date: 2019-12-17 17:15:07
 */
#include "rocksdb/db.h"
#include "rocksdb/utilities/checkpoint.h"
#include <iostream>
#include <cstdlib>
#include "util.h"
#include <fstream>

int main() {
    rocksdb::DB* db;
    rocksdb::Options options;
    options.create_if_missing = true;
    rocksdb::Status status = rocksdb::DB::Open(options, "test_rocks", &db);
    if (status.ok()) {
        std::cout<<"open the db success"<<std::endl;
    }

    std::vector<rocksdb::ColumnFamilyHandle*> tmp_handle;

    std::vector<rocksdb::ColumnFamilyDescriptor> column_families;

    rocksdb::ColumnFamilyOptions cf_options;
    column_families.emplace_back("haha", cf_options);
    column_families.emplace_back("hehe", cf_options);
    column_families.emplace_back("heha", cf_options);
    column_families.emplace_back("hahe", cf_options);
    status = db->CreateColumnFamilies(column_families, &tmp_handle);
    std::cout<<tmp_handle.size()<<std::endl;
    rocksdb::WriteBatch batch;
    for (size_t i(0); i<tmp_handle.size(); ++i) {
        for (int key(0); key<99999; ++key) {
            int value = i*1000000 + key;
            batch.Put(tmp_handle.at(i), std::to_string(key), std::to_string(value));
        }
    }
    status = db->Write(rocksdb::WriteOptions(), &batch);
    std::string value;
    status = db->Get(rocksdb::ReadOptions(), tmp_handle.at(3), std::to_string(1), &value);
    std::cout<<value<<std::endl;

    status = db->Flush(rocksdb::FlushOptions(), tmp_handle);

    rocksdb::Checkpoint* checkpoint;
    status = rocksdb::Checkpoint::Create(db, &checkpoint);

    rocksdb::ExportImportFilesMetaData* metadata;
    rocksdb::Env *env = db->GetEnv();
    env->CreateDir("test_rocks/snap");
    checkpoint->ExportColumnFamily(tmp_handle.at(1), "test_rocks/snap/haha", &metadata);


    auto j_meta = get_json_from_sstmeta(*metadata);
    std::ofstream metafile("test_rocks/snap/haha/metafile");
    // std::cout<<j_meta<<std::endl;
    metafile<<j_meta;
    metafile.close();

    system("scp -P 13122 -o StrictHostKeyChecking=no -r test_rocks/snap/haha 172.18.39.202:/data/test_ps");
    env->DeleteDir("test_rocks/snap/haha");


    // status = rocksdb::DB::Open(options, "new_rocks", &db);
    // rocksdb::ColumnFamilyHandle* test2;
    // db->CreateColumnFamilyWithImport(cf_options, "haha", rocksdb::ImportColumnFamilyOptions(), *metadata, &test2);
    // std::string value2;
    // std::string key("5");
    // status = db->Get(rocksdb::ReadOptions(), test2, key, &value2);
    // if (status.ok()) {
    //     std::cout<<"get the value ok"<<std::endl;
    // }
    // std::cout<<value2<<std::endl;


    delete db;


}